<?php
	$form = $_POST;
	$picture = new Picture();
	if ($picture_id) {
		$picture->select($picture_id);
		$picture->setdisplay_name($form["picture_name"]);
		$picture->update($picture_id);
		$msg = "Hình đã được sửa thành công";
		if (isset($tour_id))
			$redirect_url = "?act=pictures_list&tour_id=".$tour_id;
		elseif (isset($room_id))
			$redirect_url = "?act=pictures_list_for_room&room_id=".$room_id;
		elseif (isset($hotel_id))
			$redirect_url = "?act=pictures_list_for_hotel&hotel_id=".$hotel_id;
	} else {
		$prefixes = array(
			0	=>	'tour_price_',
			1	=>	'tour_ideas_',
			2	=>	'tour_ads_',
			3	=>	'loc_'
		);
		$pic_id = check_and_upload_picture("picture", $form["picture_name"], $_FILES, $prefixes);
		if ($pic_id) {
			$msg = "Hình đã được thêm thành công";
			if ($tour_id) {
				$tour = new Tour();
				$tour->select($tour_id);
				$picture_ids = $tour->getpictures();
				$picture_ids = json_decode($picture_ids, true);
				$picture_ids[] = $pic_id;
				$tour->setpictures(json_encode($picture_ids));
				$tour->update($tour_id);
				$redirect_url = "?act=pictures_list&tour_id=".$tour_id;
			} elseif (($room_id)) {
				$room = new Room();
				$room->select($room_id);
				$picture_ids = $room->getpictures();
				$picture_ids = json_decode($picture_ids, true);
				$picture_ids[] = $pic_id;
				$room->setpictures(json_encode($picture_ids));
				$room->update($room_id);
				$redirect_url = "?act=pictures_list_for_room&room_id=".$room_id;
			} elseif (($hotel_id)) {
				$hotel = new Hotel();
				$hotel->select($hotel_id);
				$picture_ids = $hotel->getpictures();
				$picture_ids = json_decode($picture_ids, true);
				$picture_ids[] = $pic_id;
				$hotel->setpictures(json_encode($picture_ids));
				$hotel->update($hotel_id);
				$redirect_url = "?act=pictures_list_for_hotel&hotel_id=".$hotel_id;
			}
		}
		
	}
	
	admin_load($msg, $redirect_url);
?>